非常簡短的背景:設置隱藏字段的值,以JavaScript變量
我使用Jquery Autocomplete查找從數據庫項目的價值。然後,該值以某種方式提供給同一表單中的隱藏字段,然後插入到數據庫中。
稍微有點複雜的是,我正在通過Jquery Ui Tabs工作,這在過去我沒有很多樂趣。
所以文件中的一些代碼,創建標籤:
<script type="text/javascript">
function findValue(li) {
// if(li == null) return alert("No match!");
// if coming from an AJAX call, let's use the CityId as the value
if(!!li.extra) var sValue = li.extra[0];
// otherwise, let's just display the value in the text box
else var sValue = li.selectValue;
}
function selectItem(li) {
findValue(li);
}
function formatItem(row) {
return row[0];
}
function lookupAjax(){
var oSuggest = $(".role")[0].autocompleter;
oSuggest.findValue();
return false;
}
function lookupLocal(){
var oSuggest = $("#role")[0].autocompleter;
oSuggest.findValue();
return false;
}
</script>
同樣的文件創建的標籤,也有一個回調發起jQuery的自動完成
<script type="text/javascript">
$(function() {
$("#tabs").tabs({
load: function(event, ui) { setTimeout(function() { $(".title").focus(); }, 500);
var ac = $(".role").autocomplete(
"/profile/autocomplete",
{
delay:10,
minChars:1,
matchSubset:1,
matchContains:1,
cacheLength:10,
onItemSelect:selectItem,
onFindValue:findValue,
formatItem:formatItem,
autoFill:true
}
);
ac[0].autocompleter.findValue();
}
});
});
</script>
然後在實際標籤代碼中是表格
<?php $tab_id = $this->uri->segment(4);
$hidden = array('tab_id' => $tab_id);
$attributes = array('name' => 'additem');
echo form_open('profile/new_item', $attributes, $hidden); ?>
<input type="hidden" value="" name="rolehidden"/>
<?php echo validation_errors(); ?>
<table width="100%" padding="0" class="add-item">
<tr>
<td>Title: <input class="title" type="text" name="title" value="<?php echo set_value('title'); ?>"></input></td>
<td>Role: <input class="role" type="text" name="role" size="15"></input></td>
<td>Year: <input type="text" name="year" size="4" maxlength="4"></input></td>
<td align="right"><input type="submit" value="Add"></input></td>
</tr>
</table>
</form>t
我想要做的就是獲取sValue並將其設置爲窗體中隱藏字段的值。
我還應該提到,JQuery選項卡有多個選項卡,都具有相同的形式。這意味着在所有選項卡中有幾個不同的輸入字段都具有相同的名稱/標識/類。
我知道這是ID屬性的問題,但不知道是否同樣適用於名稱屬性。
我已經嘗試了很多不同的Javascript和JQuery的代碼片段,我再也想不到了。
突破......但仍然是一個問題
$("[name='rolehidden']").val(sValue);
剛剛有了一個突破。此代碼確實有效......但僅限於<input ="text">
元素。它無法在<input ="hidden">
上工作是否有解決方法,或者我應該使用CSS來隱藏文本輸入框?
請幫
添
我應該把這個放在哪裏?我必須在提交按鈕上觸發這個嗎?對不起,我需要多一點幫助 – Tim 2010-01-14 04:47:33
把它放在你的'findValue()'函數結束。這就是'sValue'創建的地方,據我所知。 – Sampson 2010-01-14 04:54:00
你好, 是的,我曾經嘗試這樣做,只是試圖再次但是它不工作。 請注意,我還補充說,可能是至關重要的這個工作的詳細信息... 我還要提到的是JQuery的標籤有多個選項卡都具有相同的形式。這意味着在所有選項卡中有幾個不同的輸入字段都具有相同的名稱/標識/類。 我知道這是ID屬性的問題,但不能肯定是否同樣適用於name屬性。 – Tim 2010-01-14 05:23:36