简体   繁体   English

使用javascript在td中获取输入文本值

[英]Get input text value inside td with javascript

I've created a table by using CGridView, inside the table contain input text field which allow user to insert value. 我通过使用CGridView创建了一个表,该表内部包含允许用户插入值的输入文本字段。 The issue is I can get the text from the table where there is no input field, but not with the one that contain input field. 问题是我可以从没有输入字段的表中获取文本,但不能从包含输入字段的表中获取文本。

Php: Php:

<?php
$this->widget('zii.widgets.grid.CGridView',array(
    'id' => 'oc-list-upgradeRule',
    'dataProvider' => $upgradeRuleDataProvider,
    'selectableRows' => 2,
    'summaryText' => '',
    'columns' => array(
        array(
            'header' => '',
            'id' => 'chkbox',
            'name' => 'checkBox',
            'class' => 'CCheckBoxColumn',
        ),
        array(
            'header' => 'Min. Spending Amount',
            'id' => 'minSpendingAmount',
            'name' => 'minSpendingAmount',
            'type' => 'raw',
            'value' => 'CHTML::textField("",$data["minSpendingAmount"],array())',
            'htmlOptions'=>array(
                      'style'=>'text-align: center;'
            )
         )
    )
));
?>

Javascript: Javascript:

$(document).on('change', 'input', function () {
    var upgradeRenewDowngrade = $('.oc-upgrade-renew-downgrade');

    upgradeRenewDowngrade.find('.oc-save-upgrade-downgrade-rules').click(function (e) {
        var sel = $.fn.yiiGridView.getChecked('oc-list-upgradeRule', 'chkbox');
        var row = $.fn.yiiGridView.getRow('oc-list-upgradeRule', sel);
        var minSpendAmount = row.get(5);
        var input = $(minSpendAmount).html();

        //console.log($(input).val());
        console.log(minSpendAmount);
    })
})

From the code above, I only able to get td together with input text field. 从上面的代码中,我只能将td与输入文本字段一起获取。

<td style="text-align: center;">
    <input type="text" value="365" name id>
</td>

How to get the input text value? 如何获得输入的文字值? You may suggest other way to get the value from CGridView. 您可能建议使用其他方法从CGridView获取值。 Thanks. 谢谢。

Ok, so… 好吧...
Playing a little with your code, I ended up with this: 玩了一点您的代码,我最终得到了这一点:

 // Here, I only set my variable to be the same as yours var minSpendAmount = '<td style="text-align: center;"> <input type="text" value="365" name id></td>'; // Code you may do instead of your current console.log(minSpendAmount) var input = $(minSpendAmount).html(); console.log($(input).val()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Tell me if that helps you, or not. 告诉我是否有帮助。

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

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