2012-03-19 56 views
0

我有一個窗體,用戶需要輸入一個唯一標識符字段,當傳遞這個值時,它需要出現在不同的字段ID中。比如說。用戶輸入唯一代碼的字段被稱爲「唯一」,並且副本需要在「消息」中,我該如何實現這一點?將值分配給不同的字段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> 

感謝

+0

你可以發佈一些代碼,使其更清楚。 – ankur 2012-03-19 05:51:43

+0

嗨Ankur,感謝您的回覆,我剛剛更新了帖子,我想msg具有與引腳相同的值。 – Iyad 2012-03-19 05:54:37

回答

1

有兩種方法使用JavaScript來做到這一點。

方法1)

對唯一的字段,使得每當值被改變時,在被稱爲消息的隱藏字段它改變onchange事件。

<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')。值

如果您使用JQuery或其他JS助手框架,以上兩方面都會大大改善。

+0

嗨imothee,感謝您的回覆,在我提交表單之前,我嘗試了第二種方法,但沒有解決。該消息未被識別。 – Iyad 2012-03-19 05:58:48

+0

Hi Lyad - 你如何發佈表單?第二種方法只能使用JQuery或其他庫來執行Ajax帖子。 如果你正在使用JQuery和$(form).submit(function($。post({...})));它應該工作。但是,這隻有在您手動創建帖子值時纔有效。請參閱http://api.jquery.com/jQuery.post/ 將上面的替換爲您用於發佈的任何js框架。 – imothee 2012-03-19 06:01:50

+0

我嘗試了第一種方法,工作,謝謝 – Iyad 2012-03-19 06:04:12

0

如果您希望在標籤中同時設置您的值,則輸入該值。

你可以做一些這樣的事情。

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

function setTextValueForPins(textPin) 
{ 
$('#pinLabel').text($('#textPin').val()); 
} 

如果您希望在用戶輸入值之後設置值,則可以使用change事件。

PS:未測試代碼,讓我知道你是否面臨任何問題。