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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)