将值分配给不同的字段ID

Assign the value to different field ID?

本文关键字:字段 ID 分配      更新时间:2023-09-26

我有一个表单,其中有一个用户需要输入的唯一标识符字段,当传递该值时,它需要出现在不同的字段id中。用户在其中输入唯一代码的字段被称为"唯一",并且副本需要在"消息"中,我如何实现这一点?

<div data-role="fieldcontain">
                        <label for="pins" id="pinLabel"><span style="color:#f22300">*</span>&nbsp;Unique Code:</label>
                        <input data-mini="true" name="pins_r" id="pins" placeholder="9 alphanumeric characters"/>
                    </div>
                    <input type="hidden" id="msg" name="msg" value=pins>

感谢

使用JavaScript有两种方法。

方法1)

在唯一字段上设置onchange事件,以便每当值发生更改时,都可以在名为message的隐藏字段中进行更改。

<input type="text" id="unique" name="unique" onchange="setMessage(this);">
<input type="hidden" id="message" name="message">
function setMessage(field) {
    document.getElementById('message').value = field.value;
}

方法2)

使用ajax来发布表单,这样您就可以自己构建字段。

即。post-message=document.getElementById('unique').value

如果您使用JQuery或另一个JS助手框架,以上两个都会得到极大的改进。

如果希望同时在标签中设置值,则会输入该值。

你可以做这样的事情。

$(document).ready(function() { 
$('#pins').keypress(function() {
        setTextValueForPins(this);
    });
});
function setTextValueForPins(textPin)
{
$('#pinLabel').text($('#textPin').val());
}

如果希望在用户输入值后设置该值,则可以使用更改事件。

PS:没有测试代码,如果你遇到任何问题,请告诉我。