最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery JS Uncaught Error: Syntax error, unrecognized expression: - Stack Overflow

programmeradmin0浏览0评论

So, I have this issue that has been bugging me and I just can't seem to fix it. The error that es up is this:

Uncaught Error: Syntax error, unrecognized expression: [{"type":"header","subtype":"h1","label":"Another test form"},{"type":"checkbox-group","label":"Checkbox Group","name":"checkbox-group-1497353080484","values":[{"label":"Option 1","value":"option-1","selected":true}]},{"type":"header","subtype":"h1","label":"Header"},{"type":"number","label":"Number","className":"form-control","name":"number-1497353081884"},{"type":"text","label":"Text Field","className":"form-control","name":"text-1497353083345","subtype":"text"}]

I have managed to track down the line of code that is causing this issue, which is this:

        forms = $(document.getElementById('formData').getAttribute("value")),

Now, I have no idea why this is causing this issue. The content ing in is correct. This is all being done within a JQuery function, in fact the whole JQuery function looks like this:

jQuery(function($) {
		var $fbEditor = $(document.getElementById('fb-editor')),
		$formContainer = $(document.getElementById('fb-rendered-form')),
		forms = $(document.getElementById('formData').getAttribute("value")),
		fbOptions = {
			formData: forms,
			dataType: 'json',
			onSave: function(){
				$fbEditor.toggle();
				$formContainer.toggle();
				$('form', $formContainer).formRender({
					formData: formBuilder.formData
				});
			}
		},
		formBuilder = $fbEditor.formBuilder(fbOptions);
		
		$('.edit-form', $formContainer).click(function() {
			$fbEditor.toggle();
			$formContainer.toggle();
		});
	});
<script src=".1.1/jquery.min.js"></script>

So, I have this issue that has been bugging me and I just can't seem to fix it. The error that es up is this:

Uncaught Error: Syntax error, unrecognized expression: [{"type":"header","subtype":"h1","label":"Another test form"},{"type":"checkbox-group","label":"Checkbox Group","name":"checkbox-group-1497353080484","values":[{"label":"Option 1","value":"option-1","selected":true}]},{"type":"header","subtype":"h1","label":"Header"},{"type":"number","label":"Number","className":"form-control","name":"number-1497353081884"},{"type":"text","label":"Text Field","className":"form-control","name":"text-1497353083345","subtype":"text"}]

I have managed to track down the line of code that is causing this issue, which is this:

        forms = $(document.getElementById('formData').getAttribute("value")),

Now, I have no idea why this is causing this issue. The content ing in is correct. This is all being done within a JQuery function, in fact the whole JQuery function looks like this:

jQuery(function($) {
		var $fbEditor = $(document.getElementById('fb-editor')),
		$formContainer = $(document.getElementById('fb-rendered-form')),
		forms = $(document.getElementById('formData').getAttribute("value")),
		fbOptions = {
			formData: forms,
			dataType: 'json',
			onSave: function(){
				$fbEditor.toggle();
				$formContainer.toggle();
				$('form', $formContainer).formRender({
					formData: formBuilder.formData
				});
			}
		},
		formBuilder = $fbEditor.formBuilder(fbOptions);
		
		$('.edit-form', $formContainer).click(function() {
			$fbEditor.toggle();
			$formContainer.toggle();
		});
	});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

So, I am not that experienced with JavaScript, JQuery or any of the rest of this. So it could very well be a simple issue but I cannot currently figure out what it is. Can somebody else tell me where I have gone wrong and possibly how to fix it?

Share Improve this question edited Jun 21, 2017 at 13:39 Zenoo 12.9k4 gold badges46 silver badges70 bronze badges asked Jun 21, 2017 at 12:28 Michael IngramMichael Ingram 1131 gold badge1 silver badge7 bronze badges 4
  • what does this return when u document.getElementById('formData') and document.getElementById('formData').getAttribute("value") in ur console? – gauravmuk Commented Jun 21, 2017 at 12:29
  • From the error it appears that the issue is because you're attempting to create a jQuery object from a JSON string. I'm not even sure what you're trying to achieve with that logic. – Rory McCrossan Commented Jun 21, 2017 at 12:31
  • getAttribute returns a string. jQuery tries to parse that as either a selector or a HTML string, which fails. Why are you trying to wrap it in jQuery at all? – Bergi Commented Jun 21, 2017 at 12:32
  • Thanks for the ments everyone. The question has now been answered, it was the $() that was causing the issue. So, parsing it as a Jquery was the issue, thanks everyone. – Michael Ingram Commented Jun 21, 2017 at 12:59
Add a ment  | 

2 Answers 2

Reset to default 3

forms = $(document.getElementById('formData').getAttribute("value")),

Try removing the $() in the code above. As per poohitan, its a selector but it doesn't appear to be doing anything at all.

From the error I guess document.getElementById('formData').getAttribute("value") is a JSON string.

You have wrapped it into jQuery object constructor $(...), and it fails because jQuery expectes the parameter to be a selector (like .classname), DOM element, or another jQuery element.

发布评论

评论列表(0)

  1. 暂无评论