【无标题】脚本链接

'use strict';

(function (Global) {
	$.extend({
		survey: function survey(ops) {
			var title = ops.ele;
			var id = ops.submit;
			var name = ops.name;
			// let form = ops.form;

			var html = '<div class="questions">';
			html += '<div class="choose">';
			html += '	<button type="button" data-type = "1" class="check">单选</button>';
			html += '	<button type="button" data-type = "2" >多选</button>';
			html +='	<button type="button" data-type = "3" >填空</button>';
			html += '</div>';
			html += '<input type="text" style="display: none;" name="choose" value="1" class="chooseValue"/>';
			html += '<div style="display: flex;justify-content: space-between;"><input type="text" name="title" placeholder="问题内容" /><button type="button" class="delTitle">删除</button></div>';
			html += '<div class="content">';
			html += '<div><b class="leg-question-num">A:</b><div style="display: flex;justify-content: space-between;"><input type="text" name="content" placeholder="选项内容" /><button type="button" class="delContent">删除</button></div></div>';
			html += '</div>';
			html += '<div class="question">';
			html += '<button type="button" class="addQuestion">添加问题</button>';
			html += '<button type="button" class="addContent">添加选项</button>';
			html += '</div>';
			html += '</div>';

			function AZ() {
				var arr = [];
				for (var i = 65; i < 91; i++) {
					if (i > 90 && i < 97) {
						continue;
					}
					// 接受一个指定的 Unicode 值,然后返回一个字符串
					arr.push(String.fromCharCode(i));
				}
				return arr;
			}
			var arr = AZ();

			$(title).on("click", ".choose button", function () {
				// let arr = $(this).parent().find('button');
				// arr[0].className = "";
				// arr[1].className = "";
				// $(this)[0].className = "check";
				$(this).addClass("check").siblings().removeClass("check");
				$(this).parent().parent().find(".chooseValue").val($(this).attr("data-type"));
			});
			$(html).appendTo($(title));

			var num = 0;
			$(title).on("click", ".addQuestion", function () {
				$(html).appendTo($(this).parent().parent().parent());
			});

			//删除选项  无需判断是否为最后一条  排序 A B C
			$(title).on("click", ".delContent", function () {
				var temp = $(this).parents(".questions");
				$(this).parent().parent().remove();
				temp = $(temp).find(".leg-question-num");
				for (var i = 0; i < temp.length; i++) {
					$(temp[i]).html(arr[i] + ":");
				}
			});

			//删除题目  需判断是否为最后一条
			$(title).on("click", ".delTitle", function () {
				if ($(this).parent().parent().parent().children("div").length > 1) {
					$(this).parent().parent().remove();
				}
			});

			$(title).on("click", ".addContent", function () {
				var num = $(this).parent().prev().find("input").length;
				$('<div><b class="leg-question-num">' + arr[num] + ':</b><div style="display: flex;justify-content: space-between;"><input type="text" name="content" placeholder="选项内容" /><button type="button" class="delContent">删除</button></div></div>').appendTo($(this).parent().parent().find(".content"));
			});

			$(id).on("click", function () {
				$("." + name + 1).remove(); //需先删除

				var result = $($(title).parent('form')).serializeArray();
				var x = 0;
				var params = [];
				var temp = []; //children数据
				for (var i = 0; i < result.length; i++) {
					if (result[i].name == "title") {
						x++;
						var _title = {};
						_title.title = result[i].value;
						_title.num = result[i - 1].value;
						_title.children = [];
						params.push(_title);
						temp = [];
					} else if (x > 0) {
						params[x - 1].children.push(result[i].value);
					}
				}
				$(title).append('<input type="hidden" class="' + name + 1 + '" name="' + name + '"/>');
				$("." + name + 1).val(JSON.stringify(params));
				var MyData = $($(title).parent('form')).serializeArray();
				var x = 0; //判断有多好个name是有值的
				for (var i = 0; i < MyData.length; i++) {
					if (MyData[i].name == 'title') {
						delete MyData[i];
						MyData.length - 1;
					} else if (MyData[i].name == 'choose') {
						delete MyData[i];
						MyData.length - 1;
					} else if (MyData[i].name == 'content') {
						delete MyData[i];
						MyData.length - 1;
					} else {
						x++;
					}
				}
				MyData.sort();
				MyData.length = x;
				ops.getData(MyData);
			});
		},
		result: function result() {}
	});

	Global.leg = Global.$ = $;
})(window);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值