我有一个问题,一直困扰我一段时间。这是一个返回不一致错误消息的表单。默认情况下,表单上的复选框不是必需的,因此我强制它们使用以下代码:
$(function() {
var q = $("input[name='field_1']");
if (!$(q).is(':checked')) {
$(q).prop('required', true);
}
});
接下来出现问题,有一个隐藏的类.error
,它抓取字段的名称(例如名字),然后在此附加'is required',因此会产生用户友好的错误消息。这个类只在提交表单时出现在DOM上,似乎不喜欢复选框 - 我相信这是因为它没有任何东西放在'is required'之前。
以下是它的样子快照。
<div class="comp_class" >
<label class='label_1 '>Competition</label>
<div class="control_1">
<div class="checkbox"><label><input type='checkbox' name='field_1' value='Yes' required class ="error"/> Yes</label>
<label for="field_1" class="error"> is required.</label>
"Yes"
使用诸如名字之类的更简单的字段,错误类从label_1
中包含的HTML中获取字段名称,这在“必需”前面被拉出。默认情况下其他字段是强制性的,令人沮丧的是我无法访问HTML,否则这将非常简单,因此我尝试使用jQuery来访问和操作内容。< / p>
我尝试了类似的东西(包括许多其他东西!) -
$('#submit_button').click(function(){
$('.error').html("<p>This field is required</p>")
});
如果有人有任何建议,请按我的方式发送。
答案 0 :(得分:0)
我已经创建了一些示例代码来测试测试用例中的checkBox:
May 15, 2015 11:52:32 AM org.jboss.seam.microcontainer.TransactionManagerFactory getTransactionManager
INFO: starting JTA transaction manager
May 15, 2015 11:52:32 AM org.jboss.seam.microcontainer.DataSourceFactory getDataSource
INFO: starting Datasource at JNDI name: java:/datasource1
May 15, 2015 11:52:32 AM org.jboss.seam.microcontainer.DataSourceFactory getDataSource
INFO: starting Datasource at JNDI name: java:/DefaultDS
FAILED CONFIGURATION: @BeforeClass init
java.lang.RuntimeException: java.lang.IllegalStateException: Already installed InitialContextProperties
at com.pinncorp.mock.SeamTest.startJbossEmbeddedIfNecessary(SeamTest.java:223)
at org.jboss.seam.mock.AbstractSeamTest.startSeam(AbstractSeamTest.java:915)
at com.pinncorp.mock.SeamTest.init(SeamTest.java:133)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:17
$(document).ready(function() {
$('#submit_button').click(function() {
checkBox();
});
function checkBox() {
var $q = $("input[name='field_1']");
if (!$q.is(':checked')) {
$('.error').html("<p>This field is required</p>")
$q.prop('checked', true);
}
}
});