简体   繁体   English

在以下情况下显示输入字段 <td> 值 </td> 被点击

[英]Show an input field when <td>Value</td> is clicked

I have a table with data results displayed. 我有一张显示数据结果的表。
I want to make the values change to an input field when clicked, so that the new value can be entered and submitted. 我想在单击时将值更改为输入字段,以便可以输入和提交新值。

I have the following script which works ok at displaying the input field, but once displayed i can't click inside the input field to enter a new value. 我有以下脚本,该脚本可以很好地显示输入字段,但是一旦显示,我将无法在输入字段内单击以输入新值。

How do i prevent the input field from responding to the second click? 如何防止输入字段对第二次单击做出响应?

SCRIPT 脚本

$(".inputqty").click(function(event) {
            $(this).html('<input class="form-control input-sm" id="inputqty" style="width:50px; height:20px;" type="text" name="<?php echo $prod_var_code; ?>" value="<?php echo $qty; ?>">');
    });

RENDERED HTML 渲染HTML

<td class="inputqty" name="product1">5</td>
<td class="inputqty" name="product2">2</td>
<td class="inputqty" name="product3">8</td>

You would need to unbind or detach the event from the <td> jQuery has .unbind(). 您需要从jQuery具有.unbind()的<td>解除绑定或分离事件。

JS: JS:

$(".inputqty").click(function (event) {
    $(this).html('Whatever HTML I want');
    $(this).unbind('click');
});

Fiddle: 小提琴:

http://jsfiddle.net/YGhtm/

This will prevent a second click - it would remove the entire binding actually. 这样可以避免第二次点击-实际上会删除整个绑定。

you can use stopPropagation() to prevent parent handlers from being notified of the event, like 您可以使用stopPropagation()防止向父处理程序通知该事件,例如

$(document).on('click', 'input-sm', function(e) {
   e.stopPropagation();
});

I believe others have provided answer that will work for you. 我相信其他人已经提供了适合您的答案。 I however want to point out things that you should consider in this scenario of yours. 但是,我想指出在这种情况下应考虑的事项。

First of, unless necessary DO NOT use <?php ?> in your jquery. 首先,除非有必要,否则请勿在您的jquery中使用<?php ?> This is because PHP is server side, it gets rendered once before sent to your client. 这是因为PHP是服务器端的,在发送到客户端之前先渲染一次。 Any more interaction with the client is beyond the capabilities of your PHP server side scripting. 与客户端的任何更多交互都超出了PHP服务器端脚本的功能。

In your case, you can just take the current name attribute of your <td> for the name of your <input> and use the current value in your <td> as the value attribute of your <input> . 在您的情况下,您可以将<td>的当前name属性用作<input>的名称,并将<td>的当前值用作<input>value属性。

I personally recommend using different class name such as .editing in addition to your .inputQty to signify that it is in the middle of editing. 我个人推荐使用不同类的名称,如.editing除了你.inputQty ,以表示它是在编辑的中间。

UPDATE : I notice you use the same id as well, consider changing that to use your name attribute as well. 更新 :我注意到您也使用相同的id ,请考虑将其更改为也使用您的name属性。

From the point above, I recommend the following jsfiddle: http://jsfiddle.net/VnnXq/ 从以上的角度,我推荐以下jsfiddle: http : //jsfiddle.net/VnnXq/

$(".inputqty").click(function(event) {
    $this = $(this);
    if (!$this.hasClass('editing'))
    {
        var currentValue = $this.text();
        var name = $this.attr('name');
        $this.html('<input class="form-control input-sm" id="inputqty" style="width:50px; height:20px;" type="text" name="' + name + '" value="' + currentValue + '">');
        $this.addClass('editing');
    }  
});

Try 尝试

$(".inputqty").click(function (event) {
    var $this = $(this);
    if (!$this.has('input')) {
        $(this).html('<input class="form-control input-sm" id="inputqty" style="width:50px; height:20px;" type="text" name="<?php echo $prod_var_code; ?>" value="<?php echo $qty; ?>">');
    }
});

You can also remove the class like 您也可以remove此类

$(".inputqty").click(function(event) {
        $(this).html('<input class="form-control input-sm" id="inputqty" style="width:50px; height:20px;" type="text" name="<?php echo $prod_var_code; ?>" value="<?php echo $qty; ?>">');
        $(this).removeClass('inputqty');
});

Then the click event will not work for that.It will be the better option that if you maintain an another class ratherthan inputqty and append this event to that new class,and remove,if you want to maintain the inputqty . 那么click event将不适用于该click event如果您要维护另一个类而不是inputqty并将此事件附加到该新类上,然后删除它(如果要维护inputqty ,则将是inputqty

The above solutions work at displaying the input field, however using the php variable in the jquery script doesn't. 上面的解决方案在显示输入字段时起作用,但是在jquery脚本中不使用php变量。 So the solution i ended up using is this. 所以我最终使用的解决方案是这个。

HTML/PHP HTML / PHP

echo '<td>
      <p class="valueqty">' . $qty . '</p>
      <input style="display:none" type="text" name="' . $prod_var_code . '" value="' . $qty . '">
      </td>';

SCRIPT 脚本

$(".valueqty").click(function(event) {
            $(this).hide();
            $(this).next().show();
    });

Hope this helps someone :) 希望这可以帮助某人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM