我试图阻止用户不选择 jquery 自动完成选项。我有以下代码,它正在运行,但是当我提交表单时,'hidden_applinput_' + applid 字段值被删除。下面是代码
$(function() {
try {
$("[id^=applinput_]").each(function(){
app_id = this.id.split("_");
id = app_id[1];
$("#applinput_"+ id).autocomplete({
source: function(request, response) {
$.ajax({
url: "cfc/cfc_App.cfc?method=getMethod&returnformat=json",
dataType: "json",
data: {
nameAppSearchString: request.term,
maxRows: 25,
style: "full",
},
success: function(data) {
response(data);
}
})
},
select: function(event, ui) {
//separate id and checkbox
app_selid = this.id.split("_");
//separate id
applid = app_selid[1];
$(this).val(ui.item.label);
$('#hidden_applinput_' + applid).val(ui.item.value);
$('#typeinput_' + applid).val(ui.item.type);
$('#hidden_typeinput_' + applid).val(ui.item.typeID);
return false;
},
change: function (event, ui) {
if (!ui.item) {
this.value = '';
$('#hidden_applinput_' + applid).val('');
}
else{
// return your label here
}
},
})
})
.data( "autocomplete" )._renderItem = function( ul, item )
{
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append('<a onmouseover=$("#span' + item.value + '").show(); onmouseout=$("#span' + item.value + '").hide();><span style="float:left;" >' + item.label + '</span><span id="span' + item.value + '" style="float: right;height:inherit; font-size: 13px; font-weight: bold; padding-top: 0.3em; padding-right: 0.4em; padding-bottom: 0.3em; padding-left: 0.4em; display: none; cursor:pointer; " onclick=javascript:event.stopPropagation();showprofile("' + item.value + '");><!---view profile---></span><div style="clear:both; height:auto;"></div></a>')
.appendTo( ul );
};
} catch(exception){}
});
问题出在change事件上
$('#hidden_applinput_' + applid).val('');
如果我删除它,表单将发布值。还有其他方法吗?
编辑
我正在添加一些 HTML 代码来帮助解决这个问题。我希望尽可能简单,所以请询问您是否希望看到更多代码。这是一个管理脚本,所以我必须对某些事情保密。我正在使用 Coldfusion 和 jQuery。相关HTML/CFM代码如下。
<cfquery name="qApp2">
SELECT *
FROM AppType
WHERE (AppTypeID NOT IN (<cfqueryparam cfsqltype="cf_sql_varchar" value="#Applist#" list="yes">))
ORDER BY AppOrder
</CFQUERY>
<cfset index = 1>
<cfloop query="qApp2">
<!--- App Query --->
<cfquery name="qMasterApp">
SELECT *
FROM App
WHERE AppType = <cfqueryparam value="#AppTypeID#" cfsqltype="cf_sql_varchar">
</cfquery>
<h3 id="header_#index#">inactive - #AppType#</h3>
<div>
<p>
<!---- Serial Number --->
<div class="ctrlHolder" id="serial_#index#"><label for="" class="serial" style="display:none"><em>*</em>Serial Number</label>
<cfinput type="text"
name="app_#AppTypeID#_ser"
data-default-value="Enter Serial Number or Value"
size="35"
class="textInput"
id="serialinput_#index#"
value="" disabled />
<!---<cfinput name="app_#AppTypeID#_IDd" type="hidden" id="hserialinput_#index#" value="" disabled />--->
<p class="formHint">field is required</p>
</div>
<!--- App --->
<div class="ctrlHolder" id="appl_#index#"><label for="" style="display:none"><em>*</em>App</label>
<cfinput name="app_#AppTypeID#_app"
data-default-value="App"
class="textInput AppSearch"
id="applinput_#index#"
value="" disabled>
<cfinput name="app_#AppTypeID#_IDd" type="hidden" class="hidden_AppSearch" id="hidden_applinput_#index#" value="" />
<p class="formHint">App is required</p>
</div>
<!--- active --->
<div class="ctrlHolder" id="color_select">
<ul class="list">
<li>
<label for="agreement">
<input type="checkbox" id="checkbox2_#index#" name="app_#AppTypeID#_chk" style="width:50px">
active
</label>
</li>
<li>
<a class="dig3">[add an App]</a>
</li>
</ul>
</div>
</p>
</div>
<cfset index = index + 1>
<cfset Applist = ListAppend(Applist,AppTypeID)>
</cfloop>
最佳答案
$('#hidden_applinput_' + applid).val('');
上面的代码片段失败,因为 applid 在当前范围内未定义,即更改事件处理程序。在您的情况下,您通过添加 try..catch block 抑制了所有错误,因此 js 控制台中没有错误。
您的更改事件处理程序应如下所示:
change: function (event, ui) {
if (!ui.item) {
this.value = '';
app_selid = this.id.split("_");
applid = app_selid[1];
$('#hidden_applinput_' + applid).val('');
}
else{
// return your label here
}
},
关于javascript - 清除更改功能的表单字段会删除表单发布值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21082189/
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser