dongtanghuan1885 2011-04-25 16:31
浏览 26
已采纳

使用按钮单击功能更改复选框输入名称

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 type=\"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 :(

  • 写回答

2条回答 默认 最新

  • doulun9534 2011-04-25 16:42
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图