te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>html - How to set the value of an input hidden field to a value entered in another input text field using JavaScript? - Stack Ov
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to set the value of an input hidden field to a value entered in another input text field using JavaScript? - Stack Ov

programmeradmin3浏览0评论

I have an input textfield with the name qtyText, to which a user enters a value. I would like to set this value as the value for another hidden field named qtyTextHidden using JavaScript. How can I go about it?

HTML:

<input name = "qtyText" type = "textbox" size = "2" value = "" />
<input type="hidden"  value = "" name="qtyTextHidden"/>

My efforts to set the field value using JS works, but I am unable to send the value to the servlet. So, I am attempting to directly set the value using a function, and then trying to send it to the servlet. I would like to have a value = someJSFunction(), some kind. The function needs to trigger upon onChange event in the qtyText input field.

I have an input textfield with the name qtyText, to which a user enters a value. I would like to set this value as the value for another hidden field named qtyTextHidden using JavaScript. How can I go about it?

HTML:

<input name = "qtyText" type = "textbox" size = "2" value = "" />
<input type="hidden"  value = "" name="qtyTextHidden"/>

My efforts to set the field value using JS works, but I am unable to send the value to the servlet. So, I am attempting to directly set the value using a function, and then trying to send it to the servlet. I would like to have a value = someJSFunction(), some kind. The function needs to trigger upon onChange event in the qtyText input field.

Share Improve this question edited Dec 7, 2024 at 13:29 Roman C 1 asked Mar 9, 2013 at 15:44 RaghuRaghu 1,1615 gold badges20 silver badges41 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Using jQuery:

$(document).ready(function() {
    $('input:text[name="qtyText"]').keyup(function() {
        $('input:hidden[name="qtyTextHidden"]').val( $(this).val() );
    });
});

Using JavaScript:

window.onload = function() {
    if(document.readyState == 'plete') {
        document.getElementsByTagName('input')[0].onkeyup = function() {
            document.getElementsByTagName('input')[1].value = this.value;
        };
    }
}:

I'd suggest:

function updateHidden(valueFrom, valueTo) {
    valueTo.value = valueFrom.value;
}

var inputs = document.getElementsByTagName('input'),
    textInputs = [],
    hiddenInputs = [],
    refersTo;

for (var i = 0, len = inputs.length; i < len; i++) {
    switch (inputs[i].type) {
        case 'hidden':
            hiddenInputs.push(inputs[i]);
            break;
        case 'text':
        default:
            textInputs.push(inputs[i]);
            break;
    }
}

for (var i = 0, len = textInputs.length; i < len; i++) {
    refersTo = document.getElementsByName(textInputs[i].name + 'Hidden')[0];
    if (refersTo !== null) {
        textInputs[i].onchange = function () {
            updateHidden(this, document.getElementsByName(this.name + 'Hidden')[0]);
        };
    }
}

JS Fiddle demo.

Incidentally: there is no type="textbox". At all. Ever. Anywhere in HTML, not even in HTML 5: it's type="text". The only reason it works with type="textbox" is because browsers are ridiculously forgiving and, if the type isn't understood, it defaults to type="text".

To set the value of the hidden field the easiest way is to use the javascript function that to pass three parameters from, to, and form names:

<script type="text/javascript">
    function someJSFunction(form, from, to){
      var el_from=form[from], el_to=form[to];
      el_to.value=el_from.value;
      return el_to.value;
    }
</script>
<form name="myform">
<input name = "qtyText" type = "textbox" size = "2" value = "" onchange="someJSFunction(myform, 'qtyText', 'qtyTextHidden')"/>
<input type="hidden"  value = "" name="qtyTextHidden"/>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论