开发者

Changing an checkbox input name with a button click function

I'm running into a problem with changing the name attribute on an input element when a button is clicked.

I have 3 buttons:

* Add Renter

* Delete Customer

* Update Customer

This is the input element:

<input typ开发者_StackOverflow中文版e=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />

$busRow is the user's id..

The form is submitted to a php script that checks to see what button was set and does different actions for that button. When the user clicks the delete it loops through and deletes each customer that was in the todelete[] array. When the user clicks the Update Customer button i want to change the checkbox input's name to "id". Then my php script will grab the user's id instead of grabbing the array value and then redirect the user to the Update Customer page with the id stored on the URL.

This is the javascript i'm using at the bottom of the page:

<script type="text/javascript">
    $("button").click(function () {
        if ($(this).text() == "Update Customer") {
            $('input.check').attr("name", "id");
        }
    });
</script>

When i get to the Update Customer page the id is not there in the URL and i can't pull any values based upon the ID :(


You're setting the name value to "id", instead of the value stored in value. Try this:

$("button").click(function () {

    if ($(this).text() == "Update Customer") {
        var element = $('input.check');
        element.attr("name", element.attr("value"));
    }
 });

Although, I think you could just simply pass the value of the input tag instead of the name when the btn clicked is 'Update'. That would save you the hassle of changing these different attributes and could save you trouble.

Edit: I'm reading the OP again and see you want to set the name att to "id" and not the user's id??? You're question is vague and hard to understand what you're trying to do. You're leaving out crucial code that grabs this "id" and redirects the user.

Edit2: What's up with the semi-colon between your string concat? I hope that's not in your source code. Now seeing your redirect code. I think your problem lies in trying to change the tag's attributes/values with an 'input.check' selector. I would also say you should redesign the way you are implementing this. Your checkboxes shouldn't hold your user information. But regardless, I don't think you need to even change the name value. If update isset() then just make $id = (its current name value);
Download firebug to debug your javascript code. Firebug will also show you errors in your code that wouldn't see otherwise. It should help you out a lot


Try putting an alert inside the if statement to check if that is actually becoming true, to narrow down what might be the cause.

Also I've never seen input.check used as a selector. Perhaps give the button an id making it:

<input id=\"customercheck\" type=\"checkbox\" name=\"todelete[]\" value=\"$busRow[0]\" class=\"check\" />

Then change the attribute via the ID like so:

$('#customercheck').attr("name", "id");

that also saves you if you add another input to the field later down the road.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜