0

Sorry if the question wording is bad, I'm not really sure how to describe it.

I have a button which, when clicked, does this:

function Save(id, projectID) {
        var url = "@Url.Content("~/Config/Detail?id=")" + id + "&projectID=" + projectID;
    var _options = {
        area: ['590px', '385px'],
        content: url
    }
    tools.openTopPage(_options);
};

If I manually enter values for id and project ID in the second line, it works, so I know there's no problem in this function.

However, when the function is called from an input string it doesn't work.

The string is here (being put into a htmlArray):

    htmlArray.push(
    "<td class='text-center'>
       <input type='button' class='form-btn' value='修改' 
           onclick=Save(" + item.id + ", " + item.projectID + ") />
       <input type='button' id='btn-delete' class='form-btn'
           value='删除' onclick=Delete(" + item.id + ") />
    </td>");

I thought the problem was due to the way the input string breaks and has variables item.id and item.projectID put into the Save() function, however it doesn't even work if I keep it simple. For example, the following doesn't work either:

htmlArray.push(
    "<td class='text-center'>
       <input type='button' class='form-btn' value='修改' 
           onclick=Save(6, 3) />
       <input type='button' id='btn-delete' class='form-btn'
           value='删除' onclick=Delete(" + item.id + ") />
    </td>");

The values 6 and 3 which I have used are the values I would expect to be given if using item.id and item.projectID.

Most interesting is that the HTML renders the same in the browser in both cases. The result looks like this:

enter image description here

So surely the issue is somewhere around the Save() function, but I don't know where.

If I simplify the Save() function to take only one parameter it works (e.g. "Save(" + item.id + ")" will work fine), so this is what leads me to believe it's messing up when dealing with two parameters.

Any input much appreciated, thanks.

2
  • onclick ="save(6, '', 3)" = "" is the problem, fix it Commented Sep 19, 2017 at 14:08
  • Check your console for more Commented Sep 19, 2017 at 14:08

4 Answers 4

5

Try

onclick="save('6','3')"

" after 6, make issue

Try

onclick="save('6','3')"

" after 6, make issue

" and ' make issue in JavaScript

last option is:

var parameters=String("'" + item.id + "','"+item.projectID+"'");

use this as

htmlArray.push(
    "<td class='text-center'>
       <input type='button' class='form-btn' value='修改' 

           onclick=Save("+parameters+") />

       <input type='button' id='btn-delete' class='form-btn'
           value='删除' onclick=Delete(" + item.id + ") />
    </td>");

looks stupid but I used it many times.

If any other option,welcome !!!

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried loads of variations and none of it works. As I said, Save(6, 3) doesn't work, whereas Save(3) is fine, so it seems to only have a problem when I put more than one parameter in. In this case, the only additional punctuation I'm using is a comma, so I don't see how the quotation mark is the issue.
I'm an idiot. I managed to solve it last night using your advice, just after I posted the previous comment, but I couldn't comment again until now because stackoverflow suddenly became unavailable (probably because I'm in China... ha). Thanks for your advice, it worked, cheers!
1

https://jsfiddle.net/6h73pxcv/

<input type='button' class='form-btn' value='修改' onclick="Save(6, 3)" />

onclick needs to have quotes around it. Also make sure "Save()" is globally available

Comments

1

If your problem is with multiple parameter why not use only one and threat item as a object? So instead of breaking items you could pass it all.

function Save(item) {
        var url = "@Url.Content("~/Config/Detail?id=")" + item.id + "&projectID=" + item.projectID;
    var _options = {
        area: ['590px', '385px'],
        content: url
    }
    tools.openTopPage(_options);
};

and in htmlArray:

htmlArray.push(
    "<td class='text-center'>
       <input type='button' class='form-btn' value='修改' 
           onclick=Save(" + item + ") />
       <input type='button' id='btn-delete' class='form-btn'
           value='删除' onclick=Delete(" + item.id + ") />
    </td>");

I know it is not what you want but is another way of thinking and can help you further.

UPDATE

Maybe if you switch some single and double quotes inside your htmlArray you may achieve your goal.

htmlArray.push(
        '<td class="text-center">
           <input type="button" class="form-btn" value="修改" 
               onclick="Save(' + item + ')" />
           <input type="button" id="btn-delete" class="form-btn"
               value="删除" onclick="Delete(' + item.id + ')" />
        </td>');

4 Comments

I've tried this already. I tried making a variable {/// saveString = item.id + ", " + item.projectID ///} then used that single variable in the input string. However, this has exactly the same problematic result when rendering in the browser. It once again comes up as {/// onclick ="save(6, '', 3)" = "" ///} Most interesting is that when I call {/// alert(saveString) ///} it prints 6, 3 , so that part is correct.
@StuartAitken I made an update. Maybe if you change some quotes, you could achieve your goal.
All solved now, and yeah it was a problem with the quotes. Cheers!
@StuartAitken Glad could help ya! Good luck in your further projects! :)
0

Here you go with a solution using ES6 standard

htmlArray.push(
  `<td class='text-center'>
     <input type='button' class='form-btn' value='修改' 
       onclick="Save('${item.id}, '${item.projectID}');" />
     <input type='button' id='btn-delete' class='form-btn'
       value='删除' onclick="Delete('${item.id}');" />
  </td>`);

Hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.