从表单元素之外的输入标签读取 [英] reading from input tag outside form element

查看:94
本文介绍了从表单元素之外的输入标签读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有输入字段的网页(从日历中选择一个日期)。

问题是我需要从两个不同的表单元素中读取输入字段,每个表单元素都有自己的行动和其他价值观。

 
|日历| [做这个] [这样做]

我不想复制两种形式的输入字段,因为它看起来很傻。
有什么建议吗?

编辑:这里有两种形式:



pre> < form action =/ adm / user / credit / update?sms =<?= $ id?>& user =<?= $ user?> 方法= POST >
< input type =hiddenname =isPaidvalue =1/>
< label for =datums>基准:< / label>
< input type =textid =datumsname =datums/>
< input type =submitvalue =Paidstyle =background-color:green; color:white/>
< / form>
< form action =/ adm / user / credit / extend?sms =<?= $ id?>& user =<?= $ user?>方法= POST >
< input type =hiddenname =phonevalue =<?= $ phone?>>
< select name =period>
< option value =7> 7< / option>
< option value =14> 14< / option>
< option value =30> 30< / option>
< / select> days
< input type =submitvalue =Extendstyle =background-color:blue; color:white>
< / form>


解决方案

只需给它唯一的ID并使用 document.getElementById 就像你想要的任何地方:

  var myValue = document.getElementById( myInputID)值。 

这样,输入

编辑:

要提交时读取值,首先添加 onsubmit 部分到相关表格:

 < form action =.. ...onsubmit =ReadValue(this);> 

现在在您的页面中有这样的内容:

 < script type =text / javascript> 
函数ReadValue(oForm){
var myValue = document.getElementById(myInputID).value;
alert(value is:+ myValue);
}
< / script>

这会将该值显示为警报对话框。如果您想使用该值填充隐藏的表单字段,代之以这样的代码:
$ b $ pre $ 函数ReadValue(oForm){
var myValue = document.getElementById(myInputID )。值;
oForm.elements [myHiddenFieldId]。value = myValue;
}

希望它足够清楚! :)

I have a web page with an input field (a calendar to select a date from).
The problem is that I need to read that input field from 2 different form elements that each have their own action and other values.

|calendar|   [do this]   [do that]

I don't want to copy the input field in both forms since it would look silly. Any suggestions?

Edit: here's the two forms:

<form action="/adm/user/credit/update?sms=<?=$id?>&user=<?=$user?>" method="post">
    <input type="hidden" name="isPaid" value="1"/>
    <label for="datums">Datums: </label>
    <input type="text" id="datums" name="datums"/>
    <input type="submit" value="Paid" style="background-color:green; color:white"/>
</form>
<form action="/adm/user/credit/extend?sms=<?=$id?>&user=<?=$user?>" method="post">
    <input type="hidden" name="phone" value="<?=$phone?>">
    <select name="period">
        <option value="7">7</option>
        <option value="14">14</option>
        <option value="30">30</option>
    </select> days
    <input type="submit" value="Extend" style="background-color: blue; color: white">
</form>

解决方案

Just give it unique ID and use document.getElementById like this anywhere you want:

var myValue = document.getElementById("myInputID").value;

This way it doesn't matter where the input is located, it can even be outiside of any form.

Edit:
To read the value upon submit, first add the onsubmit part to the relevant form:

<form action="....." onsubmit="ReadValue(this);">

And now have this in your page:

<script type="text/javascript">
function ReadValue(oForm) {
   var myValue = document.getElementById("myInputID").value;
   alert("value is: " + myValue);
}
</script>

This will show the value as alert dialog.. if you want to populate hidden form field with the value, have such code instead:

function ReadValue(oForm) {
   var myValue = document.getElementById("myInputID").value;
   oForm.elements["myHiddenFieldId"].value = myValue;
}

Hope it's clear enough! :)

这篇关于从表单元素之外的输入标签读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆